const express = require('express');
const router = express.Router();
const roomService = require('../services/roomService');

// 创建房间
router.post('/create', async (req, res) => {
  try {
    const { nickname } =req.body;
    const room = await roomService.createRoom(nickname);
    console.log('服务器创建房间响应:', { success: true, room });
    res.json({ success: true, room });
  } catch (error) {
    console.error('服务器创建房间错误:', error);
    res.status(500).json({ success: false, error: error.message });
  }
});

// 加入房间
router.post('/join', async (req, res) => {
  try {
    const { roomNumber, nickname } = req.body;
    // 在这里生成新用户的 userId
    const room = await roomService.joinRoom(roomNumber, nickname);
    res.json({ success: true, room });
  } catch (error) {
    res.status(500).json({ success: false, error: error.message });
  }
});

// 获取房间信息
router.get('/info', async (req, res) => {
  try {
    const { roomNumber } = req.query;
    const room = await roomService.getRoom(roomNumber);
    res.json({ success: true, room });
  } catch (error) {
    res.status(500).json({ success: false, error: error.message });
  }
});

module.exports = router; 